package com.carbon.assets.controller;

import com.carbon.assets.entity.ExchangeAccount;
import com.carbon.assets.param.ExchangeAccountQueryParam;
import com.carbon.assets.service.ExchangeAccountService;
import com.carbon.assets.vo.ExchangeAccountQueryVo;
import com.carbon.assets.common.BaseController;
import com.carbon.domain.common.ApiResult;
import com.carbon.common.api.Paging;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 交易所账户表 前端控制器
 * </p>
 *
 * @author Carbon
 * @since 2022-09-26
 */
@Slf4j
@RestController
@RequestMapping("/assets/exchangeAccount")
@Api(value = "交易所账户管理模块", description = "交易所账户管理模块", 
     tags = {"交易所账户管理模块"})
public class ExchangeAccountController extends BaseController {

    @Autowired
    private ExchangeAccountService exchangeAccountService;

    /**
     * 测试接口
     */
    @GetMapping("/test")
    @ApiOperation(value = "测试接口", notes = "测试接口")
    public ApiResult<String> test() {
        return ApiResult.ok("ExchangeAccountController is working!");
    }

    /**
     * 分页查询交易所账户
     */
    @PostMapping("/getPageList")
    @ApiOperation(value = "分页查询交易所账户", notes = "分页查询交易所账户")
    public ApiResult<Paging<ExchangeAccountQueryVo>> getPageList(@RequestBody ExchangeAccountQueryParam queryParam) {
        try {
            log.info("查询交易所账户参数: {}", queryParam);
            Paging<ExchangeAccountQueryVo> paging = exchangeAccountService.getPageList(queryParam);
            return ApiResult.ok(paging);
        } catch (Exception e) {
            log.error("查询交易所账户失败", e);
            return ApiResult.fail("查询失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取交易所账户详情
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "获取交易所账户详情", notes = "根据ID获取交易所账户详情")
    public ApiResult<ExchangeAccountQueryVo> getInfo(@PathVariable("id") Long id) {
        try {
            log.info("获取交易所账户详情ID: {}", id);
            ExchangeAccountQueryVo vo = exchangeAccountService.getInfo(id);
            if (vo == null) {
                return ApiResult.fail("账户不存在");
            }
            return ApiResult.ok(vo);
        } catch (Exception e) {
            log.error("获取交易所账户详情失败", e);
            return ApiResult.fail("获取详情失败: " + e.getMessage());
        }
    }

    /**
     * 绑定交易账户
     */
    @PutMapping("/binding")
    @ApiOperation(value = "绑定交易账户", notes = "绑定交易账户")
    public ApiResult<Boolean> bindAccount(@RequestBody ExchangeAccount exchangeAccount) {
        try {
            log.info("绑定交易账户参数: {}", exchangeAccount);
            Boolean result = exchangeAccountService.bindAccount(exchangeAccount);
            return ApiResult.ok(result);
        } catch (Exception e) {
            log.error("绑定交易账户失败", e);
            return ApiResult.fail("绑定失败: " + e.getMessage());
        }
    }
}